package com.qf.controller;

import com.github.pagehelper.PageInfo;
import com.qf.VO.PageUtil;
import com.qf.entity.AdminUser;
import com.qf.service.AdminUserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;


@Controller
@RequestMapping("/admin")
public class AdminUserController {

    @Autowired
    private AdminUserService adminUserService;

    //登录
    @RequestMapping("/login")
    @ResponseBody
    public String login(@RequestBody AdminUser adminUser, HttpSession session){

        AdminUser adminUser1 = adminUserService.login(adminUser);

        if(adminUser1!=null){
            if(adminUser1.getPassword().equals(adminUser.getPassword())){
                session.setAttribute("adminUser",adminUser1);
                return "success";
            }else {
                return "false";
            }
        }else{
            return "err";
        }
    }

    //用户表格
    @RequestMapping("/table")
    @ResponseBody
    public PageUtil queryAll(int page, int limit ,Integer id, String email, Boolean status){

        PageInfo<AdminUser> adminUserPageInfo = adminUserService.queryAll(page, limit,id,email,status);
        PageUtil pageUtil = new PageUtil();
        pageUtil.setCode(0);
        pageUtil.setMsg("ok");
        pageUtil.setCount(adminUserPageInfo.getTotal());
        pageUtil.setData(adminUserPageInfo.getList());
        return pageUtil;

    }

    //退出登录
    @RequestMapping("/logout")
    @ResponseBody
    public String logout(HttpSession session){
        session.removeAttribute("adminUser");
        return "success";
    }

    //删除用户
    @RequestMapping("/del")
    @ResponseBody
    public int delAdminUser(int ids[]){
        int delAdminUser = adminUserService.delAdminUser(ids);
        return delAdminUser;
    }

    //添加用户
    @RequestMapping("/add")
    @ResponseBody
    public int addAdminUser(@RequestBody AdminUser adminUser){
        adminUser.setStatus(true);
        int result = adminUserService.insertUser(adminUser);
        return  result;
    }

    @RequestMapping("/update")
    @ResponseBody
    public int updateAdminUser(@RequestBody AdminUser adminUser){
        System.out.println("1111");
        int updateUser = adminUserService.updateUser(adminUser);
        return updateUser;
    }

}
